package com.example.ch07okhtttp;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.*;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/11/29 11:41
 */
@Controller
public class DownLoad {

    @RequestMapping("/DownFile")
    public void downFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取图片路径
        String img = "D:\\Files\\h3.jpg";
        //获取本地文件输入流
        InputStream in = new FileInputStream(img);
        //获取网络输入流
        OutputStream out = response.getOutputStream();
        //循环读写
        byte[] data = new byte[512];
        int len = -1;
        while ((len=in.read(data,0,data.length))!=-1){
            out.write(data, 0, len);
            out.flush();
        }
        //关闭流
        out.close();
        in.close();


    }



}
